home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / BELCH!_P / ASNDPLAY.C next >
Text File  |  1990-08-21  |  5KB  |  185 lines

  1. /* ###################################################################
  2.  
  3. MODIFICATIONS MADE BY ANDREW WELCH:
  4.  
  5. This Asynch sound unit was designed to work from within an application.
  6. In order to make it work from within a code resource, I had to make
  7. minor modifications to it.
  8.  
  9. In the callback routine, this unit set up A5 so that it could
  10. reference the static variable gSndPlaying.  Well in code resources,
  11. THINK C references static variables off of register A4, not A5 so
  12. I had to find an alternative method to find and set the gSndPlaying
  13. flag.
  14.  
  15. The method I chose should work from within a code resource, OR an
  16. application.  I simply remember the address of the global gSndPlaying
  17. and reference that address directly.  No need to remember/setup any
  18. registers.
  19.  
  20. My thanks and appologies (because I changed his code around) to
  21. Robert L Mathews for this THINK C Asynch sound unit.
  22.  
  23. ################################################################### */
  24.  
  25.  
  26. /*===========================================================
  27.  
  28.     File: ASndPlayer   ⌐1990 by Robert L Mathews / L Products
  29.     Plays asynchronous sounds when given a 'snd ' handle.
  30.  
  31.     Based upon a Pascal source example by Larry Rosenstein
  32.     of Apple Computer, Inc.
  33.  
  34.     History:
  35.     1.0        2/04/90        original version
  36.     1.1        2/08/90        fixed callback bug, added code to
  37.                         allow waiting until previous sound
  38.                         is finished
  39.     1.2        2/09/90        fixed multifinder bug - SndTask now
  40.                         adds a callBackCmd (as well it should)
  41.     1.3        2/20/90        Figured out correct way to call A5 routines
  42.                         Removed callBackCmd from SndTask
  43.         
  44. ===========================================================*/
  45.  
  46. #include <SoundMgr.h>
  47.  
  48. /*=========================================================*/
  49.  
  50. void            AInitSnd (void);
  51. OSErr            ASndPlay (Handle,Boolean);
  52. void            AStopSnd (Boolean);
  53. Boolean            SndIsPlaying (void);
  54. void            SndTask (void);
  55. pascal    void    CallBack (SndChannelPtr,SndCommand*);
  56.  
  57. static    SndChannelPtr    gSndChannel;
  58. static    Boolean            gSndPlaying;
  59. static    SndCommand        myCommand;
  60.  
  61. /* CHANGE MADE BY ANDREW WELCH: */
  62.  
  63. typedef    Boolean *BooleanPtr;
  64.  
  65. /* END OF CHANGES */
  66.  
  67.  
  68. /*=========================================================*/
  69.  
  70. #define        TRUE   1
  71. #define        FALSE  0
  72. #define        NIL    0
  73.  
  74. /*=========================================================*/
  75. /*   AInitSnd should be called as the program starts up    */
  76.  
  77. void AInitSnd (void)
  78.  
  79. {
  80.     gSndChannel = 0;
  81.     gSndPlaying = FALSE;
  82. }
  83.  
  84.  
  85. /*=========================================================*/
  86. /*       SndIsPlaying returns TRUE if a snd is in progress    */
  87.  
  88. Boolean SndIsPlaying (void)
  89.  
  90. {
  91.     return ( gSndPlaying);
  92. }
  93.  
  94.  
  95. /*=========================================================*/
  96. /*     CallBack marks the sound as done for use by SoundTask  */
  97.  
  98. pascal    void    CallBack (tempChannel, tempCmd)
  99.  
  100. SndChannelPtr    tempChannel;
  101. SndCommand        *tempCmd;
  102.  
  103. {
  104.  
  105. /* CHANGE MADE BY ANDREW WELCH: */
  106.  
  107.     BooleanPtr    flag;
  108.     
  109.     flag=(BooleanPtr)tempCmd->param2;
  110.     
  111.     (*flag)=FALSE;
  112.  
  113. /* END OF CHANGES */
  114.  
  115. }
  116.  
  117.  
  118. /*=========================================================*/
  119. /*     SndTask disposes the channel if necessary (so other    */
  120. /*     applications running can use the Sound Manager).       */
  121.  
  122. void    SndTask (void)
  123.  
  124. {
  125.     if (gSndChannel != 0 && gSndPlaying == FALSE)
  126.     {
  127.         SndDisposeChannel (gSndChannel, TRUE);
  128.         gSndChannel = 0;
  129.     }    
  130. }
  131.  
  132.  
  133. /*=========================================================*/
  134. /* ASndPlay is the async version of SndPlay, pass a handle */
  135. /* to a 'snd ' resource.  If stopPrev is TRUE, it stops    */
  136. /* any sound that may already be playing.  Otherwise, it   */
  137. /* takes control of the computer until the first sound is  */
  138. /* done (aka synchronous sound playing), and then plays    */
  139. /* the second sound asynchronously.                           */
  140.  
  141. OSErr ASndPlay (mySndHandle, stopPrev)
  142.  
  143. Handle    mySndHandle;
  144. Boolean    stopPrev;
  145.  
  146. {
  147.     OSErr    err = noErr;
  148.     AStopSnd( stopPrev);
  149.     err = SndNewChannel( &gSndChannel, 0, 0, CallBack);
  150.     if (err == noErr)
  151.     {
  152.         err = SndPlay (gSndChannel, mySndHandle, FALSE);
  153.         if (err == noErr)
  154.         {
  155.             gSndPlaying = TRUE;
  156.             myCommand.cmd = callBackCmd;
  157.  
  158. /* CHANGE MADE BY ANDREW WELCH: */
  159.  
  160.             myCommand.param2 = (long)&gSndPlaying;
  161.  
  162. /* END OF CHANGES */
  163.  
  164.             err = SndDoCommand (gSndChannel, &myCommand, FALSE);
  165.         }
  166.     }
  167.     return (err);
  168. }
  169.     
  170. /*=========================================================*/
  171. /*   AStopSnd stops an asyncronous sound from playing.     */
  172. /*   It also must be called at the end of the program.     */
  173. /*   If stopNow is TRUE, it stops the current sound immed- */
  174. /*   iately.  Otherwise, it waits (here) until the sound   */
  175. /*   is finished playing, then exits.                      */
  176.  
  177. void AStopSnd (stopNow)
  178.  
  179. Boolean    stopNow;
  180.  
  181. {
  182.     SndDisposeChannel (gSndChannel, stopNow);
  183.     gSndChannel = 0;
  184.     gSndPlaying = FALSE;
  185. }